$(function() {
    bou.init();
    $("#Search").button({
        text: true,
        icons: {
            primary: "ui-icon-search"
        }
    });

    $(window).hashchange(bou._Search);
});

var bou =
{
    init: function() {
        //binding events.
        $('#content a[delsn]').live("click", function() {
            bou.delConfirm($(this).attr("delsn"));
        })
        $('#pageing').change(bou.Search);
        $("#Search").click(function() {
            $("#pageing").val('1');
            bou.Search();
        });
        //set default values.
        utility.formatPaging($('#pageing'), 1, 1);
        //set init action
        if (utility.getObjFromHash() != null) {
            bou._Search();
        }
        //set default event.
        Utils.textBoxsOnEnter(function() { $("#Search").trigger('click'); });
    }
    , delConfirm: function(sn) {
        bou.currentSN = parseInt(sn, 10);
        var objfun = function() {
            bou.doDel(sn);
        }
        utility.showPopUp(m.Msg0003, 3, objfun);
    }
    , doDel: function(x) {
        var param = { sn: x };
        var cb = function() { window.parent.location.reload(); }
        utility.ajaxext("BouService/Delete", param, "POST", cb);
    }
    , Search: function() {
        var param = { page: $('#pageing').val(), searchkey: $('#searchkey').val() };
        utility.setObjToHash(param);
    }
    , _Search: function() {
        var param = utility.addPageToPara(utility.getObjFromHash());
        var chks = new Array("page");
        var hasparam = utility.isHashVerfied(chks, param);
        if (hasparam) {
            utility.service('BouService/Search', param, 'POST', function(data) {
                if (data.code > 0) {
                    if (data.code > 1) {
                        $('#bolisting').html('<tr><td colspan="3" class="align-center">' + data.msg + '</td></tr>');
                        utility.formatPaging($('#pageing'), 1, 1);
                    } else {
                        var currentpage = (parseInt(param['page'], 10) < data.tp) ? parseInt(param['page'], 10) : data.tp;
                        var templatepara = { 'pList': data.d, 'g': g, 'pg': currentpage };
                        utility.template("bouser/bolisting.html", function(template) {
                            $('#bolisting').html(template.process(templatepara));
                            utility.formatPaging($('#pageing'), data.tp, currentpage);
                            PageInitial.InitialPopupDiv();
                        }, "bolisting");
                    }
                } else {
                    utility.showPopUp(data.msg, 1);
                }
            });
        } else {
            this.Search();
        }
    }
};
